// JavaScript Document
(function($) {
    jQuery.fn.twWebNavigate = function(settings) {
        var defaults = { dataURL: "list-sideNavigate.txt", target: "#holder", titleLoad: "Loading list..", messageLoad: "Please wait few second...", nameID: "menu1" }
        var options = jQuery.extend(defaults, settings);
        var holder = $(this);
        $.getJSON(options.dataURL, function(data) {
            $(holder).append('<ul style="float:left; width:200px; height:500; margin:0px" id="navigate"></ul>')
            $(data).each(function(i) {
                $(holder).find("ul#navigate").append('<li class="webNaviHead ui-state-hover ui-corner-all"><b>' + data[i].caption + '</b></li><ul class="ui-state-default" style="margin:0px; padding:0px"></ul>')
                $(data[i].child).each(function(j) {
                    $("#navigate ul:eq(" + i + ")").append('<li class="webNaviItem ui-helper-clearfix" style="padding:4px" link="' + data[i].child[j].link + '" id="' + data[i].child[j].id + '"><div style="float:left">' + data[i].child[j].caption + '</div><div class="new" style="float:right; padding-left:2px; padding-right:2px"></div></li>')
                    if (data[i].child[j].New == true) {
                        $("#navigate ul:eq(" + i + ")").find("div.new:eq(" + j + ")").append('new').addClass("ui-state-error ui-corner-all")
                    }
                })
                /*				  
                $(holder).append('<ul style="float:left; width:200px; height:500; margin:0px" id="navigate"></ul>')
                $("ul#navigate").append('<li class="webNaviHead ui-state-hover ui-corner-all"><b>'+data[i].caption+'</b></li><ul class="ui-state-default"></ul>')
				
					$(data[i].child).each(function(j){
                $("ul#navigate ul:eq("+i+")").append('<li class="webNaviItem" style="padding:4px" link="'+data[i].child[j].link+'" id="'+data[i].child[j].id+'">'+data[i].child[j].caption+'<div style="float:right; padding-left:2px; padding-right:2px"></div></li>')
                if(data[i].child[j].New == true){
                $("ul#navigate ul:eq("+i+")").find("div:eq("+j+")").append('new').addClass("ui-state-error ui-corner-all")	
                }
                })
                */
            })
            var menuholder = $(holder).find("ul#navigate");
            twWebNavigateItem(menuholder, options.target, options.titleLoad, options.messageLoad, options.nameID)
        })

        function twWebNavigateItem(holder, target, titleLoad, messageLoad, nameID) {
            $(holder).css({ "list-style": "none", "padding": "0px" }).find("li.webNaviHead").next().css({ "list-style": "none", "padding": "5px" }).hide();


            $(holder).find("li").css({ "cursor": "pointer" });
            $(holder).find("li.webNaviItem").hover(function() {
                $(this).addClass("ui-state-hover").siblings().removeClass("ui-state-hover")
            }).click(function() {
                $(this).addClass("ui-state-focus").siblings().removeClass("ui-state-focus")
                var isUrl = $(this).attr("link");
                //				$("body").block({//block ui
                //					theme:     true, // true to enable jQuery UI support 
                //					title:    titleLoad,  // only used when theme == true 
                //					message:  messageLoad
                //				});
                $(target).load(isUrl, function() {
                    //$("body").unblock();							  
                })

                //switchPage(target,isUrl,true,true);
            })
            $(holder).find("li.webNaviHead").css({ "padding": "10px" }).hover(function() {
                $(this).siblings()
            }).click(function() {
                $(this).next().slideToggle().prev().siblings("li").next().slideUp()
            })
            //$(holder).find("li.webNaviHead:eq("+munuIndex+")").click();
            //$(holder).find("li#"+nameID).click();
            $(holder).find("li#" + nameID).parent().prev().click();
            $(holder).find("li#" + nameID).click();
        }

    }

})(jQuery);